﻿using System;
using System.Diagnostics;
using System.Web.Services;

namespace Morus.Presentation
{
    public abstract class WebServiceView : WebService, IView
    {
        private readonly bool _throwExceptionIfNoPresenterBound;
        private readonly PresenterBinder _presenterBinder;

        protected WebServiceView()
            : this(true)
        {
        }

        protected WebServiceView(bool throwExceptionIfNoPresenterBound)
        {
            _throwExceptionIfNoPresenterBound = throwExceptionIfNoPresenterBound;
            _presenterBinder = new PresenterBinder(this);
            _presenterBinder.PerformBinding();
        }

        public bool ThrowExceptionIfNoPresenterBound
        {
            get
            {
                return _throwExceptionIfNoPresenterBound;
            }
        }

        public event EventHandler Load;

        protected virtual void OnLoad()
        {
            if (Load != null)
            {
                Load(this, EventArgs.Empty);
            }
        }

        protected void ReleaseView()
        {
            _presenterBinder.Release();
        }
    }
}